﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FeatureModelCore.FMAnalyzer;
using System.Collections;

namespace FeatureModelTool.Windows
{

    /// <summary>
    /// Interaction logic for AllProducts.xaml
    /// </summary>
    public partial class AllProductsWindows : Window
    {
        private readonly static Brush _COMMON_COLOR = Brushes.LightSteelBlue;
        private readonly static Brush _NO_COLOR = Brushes.LightCoral;
        private readonly static Brush _YES_COLOR = Brushes.LightGreen;
        class BindItem { public int Number { get; set; } public string Value { get; set; } }
        class PositionInformation
        {
            public double LeftMargin { get; set; } 
            public double Width { get; set; }
            public int Position { get; set; }
        }
        private static readonly int _MARGIN = 12;
        private static readonly int _NUMCOL_WIDTH = 25;
        private static readonly int _HEIGHT = 23;
        private static readonly int _PIXEL_EACHCHAR = 8;
        public AllProductsWindows(FMProductCollection allProducts)
        {
            InitializeComponent();

            //List<BindItem> items = new List<BindItem>();
            //int count = 1;
            //foreach (FMProduct prod in allProducts.AllProducts)
            //{
            //    items.Add(new BindItem()
            //    {
            //        Number = count++,
            //        Value = prod.ToString()
            //    });
            //}

           // this.dataGrid1.ItemsSource = items;
            loadProductCollection(allProducts);
        }

        private void loadProductCollection(FMProductCollection allProducts)
        {
            // set all products information
            double vmargin = _MARGIN + _NUMCOL_WIDTH;
            double hMargin = _MARGIN;
            Hashtable hashtable = new Hashtable();
            int count = 0;
            PositionInformation[] posInfoList = new PositionInformation[allProducts.AllProductItem.Length];
            foreach (FMProductItem item in allProducts.AllProductItem)
            {
                double width = item.ItemName.Length * _PIXEL_EACHCHAR + _MARGIN;

                Label newLabel = generateBox(item.ItemName
                    , vmargin, hMargin, _HEIGHT, width, _COMMON_COLOR);
                this.MainGrid.Children.Add(newLabel);
                PositionInformation posInfo = new PositionInformation()
                {
                    LeftMargin = vmargin,
                    Width = width,
                    Position = count
                };
                hashtable.Add(item.ItemName, posInfo);
                posInfoList[count] = posInfo;

                vmargin += width;
                count++;
            }
            List<bool[]> mapList = new List<bool[]>();

            foreach (FMProduct product in allProducts.AllProducts)
            {
                bool[] map = new bool[allProducts.AllProductItem.Length];
                for (int i = 0; i < map.Length; ++i)
                {
                    map[i] = false;
                }
                foreach (FMProductItem item in product.Items)
                {
                    PositionInformation posInfo = (PositionInformation)hashtable[item.ItemName];
                    map[posInfo.Position] = true;
                }
                mapList.Add(map);
            }


            hMargin += _HEIGHT;
            count = 1;
            foreach (bool[] map in mapList)
            {
                vmargin = _MARGIN;
                Label numbox = generateBox(count.ToString(), vmargin, hMargin
                    , _HEIGHT, _NUMCOL_WIDTH, _COMMON_COLOR);
                this.MainGrid.Children.Add(numbox);
                for (int i = 0; i < map.Length; ++i)
                {
                    double width = posInfoList[i].Width;
                    vmargin = posInfoList[i].LeftMargin;
                    string text = "No";
                    Brush background = _NO_COLOR;
                    if(map[i])
                    {
                        text = "Yes";
                        background = _YES_COLOR;
                    }
                    Label label = generateBox(text, vmargin, hMargin, _HEIGHT, width
                        , background);
                    this.MainGrid.Children.Add(label);
                }
                count++;
                hMargin += _HEIGHT;
            }

        }

        private Label generateBox(string text, double vMargin, double hMargin
            , double height, double width, Brush background)
        {
            Label newLabel = new Label()
            {
                Content = text
            };

            newLabel.Margin = new Thickness(vMargin, hMargin, 0, 0);
            newLabel.Width = width;
            newLabel.Height = height;
            newLabel.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            newLabel.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            newLabel.Background = background;

            newLabel.BorderBrush = Brushes.Black;
            newLabel.BorderThickness = new Thickness(1, 1, 1, 1);

            return newLabel;
        }
    }
}
